package com.hangwei.paperless.fragment.sub;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hangwei.paperless.BigPicActivity;
import com.hangwei.paperless.EditSubSetListActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.adapter.HorizontalPicAdapter;
import com.hangwei.paperless.model.bean.CardToSeal;
import com.hangwei.paperless.model.bean.SubSetInfo;
import com.hangwei.paperless.model.bean.SubSetListSealBean;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.NFCCallBack;
import com.hangwei.paperless.view.SealHelper;
import com.twelve.all.base.LoadFragment;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/12/10.
 */

public class SubEditSetListFragment extends LoadFragment {
    private SubSetInfo mSubSetInfo;
    private EditText mEtActual;
    private EditText mEtOutActual;
    private EditText mEtInnerActual;
//    private MainBottomPopupWindow mMainBottomPopupWindow;
    private RelativeLayout mFlUserSeal;
    private ArrayList<String> mPicList;
    private EditSubSetListActivity mActivity;

    @SuppressLint("SetTextI18n")
    @Override
    protected View onRealCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mSubSetInfo = (SubSetInfo) arguments.getSerializable("item");
            mPicList = arguments.getStringArrayList("picList");
            mActivity = (EditSubSetListActivity) getActivity();
        }
        View inflate = inflater.inflate(R.layout.page_sub_edit_set_list, container, false);
        TextView tvSeq = inflate.findViewById(R.id.tvSeq);
        TextView tvSeq1 = inflate.findViewById(R.id.tvSeq1);
        TextView tvQty = inflate.findViewById(R.id.tvQty);
        TextView tvLimit = inflate.findViewById(R.id.tvLimit);
        mEtActual = inflate.findViewById(R.id.etActual);
        TextView tvOutFace = inflate.findViewById(R.id.tvOutFace);
        mEtOutActual = inflate.findViewById(R.id.etOutActual);
        TextView tvInnerFace = inflate.findViewById(R.id.tvInnerFace);
        mEtInnerActual = inflate.findViewById(R.id.etInnerActual);
        mFlUserSeal = inflate.findViewById(R.id.flUserSeal);
        TextView tvRemark = inflate.findViewById(R.id.tvRemark);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        if (mSubSetInfo.isComplete) {
            mEtActual.setEnabled(false);
            mEtOutActual.setEnabled(false);
            mEtInnerActual.setEnabled(false);
        } else {
            mEtActual.setEnabled(true);
            mEtOutActual.setEnabled(true);
            mEtInnerActual.setEnabled(true);
        }

        inflate.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tvSeq.setText(Tools.checkedString(mSubSetInfo.seq));
        tvSeq1.setText(Tools.checkedString(mSubSetInfo.location));
        tvQty.setText(Tools.checkedString(mSubSetInfo.qty));
        if (TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.inlMin)) && !TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.inlMax))) {
            tvLimit.setText(Tools.checkedString(mSubSetInfo.inlMax) + "Max");
        } else if (!TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.inlMin)) && TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.inlMax))) {
            tvLimit.setText(Tools.checkedString(mSubSetInfo.inlMin) + "Min");
        } else if (!TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.inlMin)) && !TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.inlMax)))
            tvLimit.setText(Tools.checkedString(mSubSetInfo.inlMin) + "-" + Tools.checkedString(mSubSetInfo.inlMax));
        else
            tvLimit.setText("--");
        mEtActual.setText(Tools.checkedString(mSubSetInfo.inlActual));
        if (TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.outerMin)) && !TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.outerMax))) {
            tvOutFace.setText(Tools.checkedString(mSubSetInfo.outerMax) + "Max");
        } else if (!TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.outerMin)) && TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.outerMax))) {
            tvOutFace.setText(Tools.checkedString(mSubSetInfo.outerMin) + "Min");
        } else if (!TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.outerMin)) && !TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.outerMax)))
            tvOutFace.setText(Tools.checkedString(mSubSetInfo.outerMin) + "-" + Tools.checkedString(mSubSetInfo.outerMax));
        else
            tvOutFace.setText("--");
//        mEtOutActual.setText(TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.outerActual))?"--":Tools.checkedString(mSubSetInfo.outerActual));
        mEtOutActual.setText(Tools.checkedString(mSubSetInfo.outerActual));
        if (TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.innerMin)) && !TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.innerMax))) {
            tvInnerFace.setText(Tools.checkedString(mSubSetInfo.innerMax) + "Max");
        } else if (!TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.innerMin)) && TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.innerMax))) {
            tvInnerFace.setText(Tools.checkedString(mSubSetInfo.innerMin) + "Min");
        } else if (!TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.innerMin)) && !TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.innerMax)))
            tvInnerFace.setText(Tools.checkedString(mSubSetInfo.innerMin) + "-" + Tools.checkedString(mSubSetInfo.innerMax));
        else
            tvInnerFace.setText("--");
//        mEtInnerActual.setText(TextUtils.isEmpty(Tools.checkedString(mSubSetInfo.inlActual))?"--":Tools.checkedString(mSubSetInfo.inlActual));
        mEtInnerActual.setText(Tools.checkedString(mSubSetInfo.inlActual));
        tvRemark.setText(Tools.checkedString(mSubSetInfo.remark));
        LinearLayoutManager layout = new LinearLayoutManager(mActivity);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layout);
        recyclerView.setAdapter(new HorizontalPicAdapter(mPicList));
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.sdv) {
                    Intent intent = new Intent(mActivity, BigPicActivity.class);
                    intent.putExtra("list", mPicList);
                    intent.putExtra("position", position);
                    startActivity(intent);
                }
            }
        });
        SealHelper sealHelper = new SealHelper(mActivity) {
            @Override
            protected void onSeal() {
                seal();
            }
        };
        sealHelper.bindData(new SealHelper.SealBean(true, mSubSetInfo.technicianStampId, mSubSetInfo.technicianId, mSubSetInfo.technicianStampTypeCode, mSubSetInfo.technicianStampCode, mSubSetInfo.technicianDate, mSubSetInfo.technician, true));
        mFlUserSeal.removeAllViews();
        mFlUserSeal.addView(sealHelper.getItemView());
        return inflate;
    }


    private void seal() {
        mActivity.sealByCard(mSubSetInfo.id, new NFCCallBack() {
            @Override
            public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                if (stampIds == null || stampIds.size() == 0) {
                    ToastUtil.showCenterToast("无印章！");
                } else {
                    final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                    loadingDialog.show();
                    String actual = mEtActual.getText().toString().trim();
                    String outActual = mEtOutActual.getText().toString().trim();
                    String innerActual = mEtInnerActual.getText().toString().trim();
                    String userId0 = SPUtil.getString(SPKey.Account.USER_ID);
                    StaticPresenter.stampBushInstallationSheet(userId0, mSubSetInfo.id, actual, outActual, innerActual, userId, stampIds.get(0).stampId, new CallBack<SubSetListSealBean>() {
                        @Override
                        public void onCompleted(SubSetListSealBean o) {
                            loadingDialog.dismiss();
                            mSubSetInfo.technicianStampId = o.technicianStampId;
                            mSubSetInfo.technicianId = o.technicianId;
                            mSubSetInfo.technicianStampTypeCode = o.technicianStampTypeCode;
                            mSubSetInfo.technicianStampCode = o.technicianStampCode;
                            mSubSetInfo.technicianDate = o.technicianDate;
                            mSubSetInfo.technician = o.technician;
                            SealHelper sealHelper = new SealHelper(mActivity) {
                                @Override
                                protected void onSeal() {
                                    seal();
                                }
                            };
                            sealHelper.bindData(new SealHelper.SealBean(true, o.technicianStampId, o.technicianId, o.technicianStampTypeCode, o.technicianStampCode, o.technicianDate, o.technician, true));
                            mFlUserSeal.removeAllViews();
                            mFlUserSeal.addView(sealHelper.getItemView());
//                            mMainBottomPopupWindow.dismiss();
                        }

                        @Override
                        public void onError() {
                            loadingDialog.dismiss();
                        }
                    });
                }
            }
        });
//        if (App.stamps != null && App.stamps.size() > 0) {
//            //有印章
//            View inflate = LayoutInflater.from(mActivity).inflate(R.layout.pop_seal, null);
//            mMainBottomPopupWindow = new MainBottomPopupWindow(mActivity, inflate, BottomPopType.Seal);
//            mMainBottomPopupWindow.setNeedAlpha(false);
//            mMainBottomPopupWindow.setOnSealListener(new MainBottomPopupWindow.OnSealListener() {
//                @Override
//                public void onSeal(String stampId) {
//                    final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
//                    loadingDialog.show();
//                    String actual = mEtActual.getText().toString().trim();
//                    String outActual = mEtOutActual.getText().toString().trim();
//                    String innerActual = mEtInnerActual.getText().toString().trim();
//                    String userId = SPUtil.getString(SPKey.Account.USER_ID);
//                    StaticPresenter.stampBushInstallationSheet(userId, mSubSetInfo.id, actual, outActual, innerActual, userId, stampId, new CallBack<SubSetListSealBean>() {
//                        @Override
//                        public void onCompleted(SubSetListSealBean o) {
//                            loadingDialog.dismiss();
//                            mSubSetInfo.technicianStampId = o.technicianStampId;
//                            mSubSetInfo.technicianId = o.technicianId;
//                            mSubSetInfo.technicianStampTypeCode = o.technicianStampTypeCode;
//                            mSubSetInfo.technicianStampCode = o.technicianStampCode;
//                            mSubSetInfo.technicianDate = o.technicianDate;
//                            SealHelper sealHelper = new SealHelper(mActivity) {
//                                @Override
//                                protected void onSeal() {
//                                    seal();
//                                }
//                            };
//                            sealHelper.bindData(new SealHelper.SealBean(true, o.technicianStampId, o.technicianId, o.technicianStampTypeCode, o.technicianStampCode, o.technicianDate, true));
//                            mFlUserSeal.removeAllViews();
//                            mFlUserSeal.addView(sealHelper.getItemView());
//                            mMainBottomPopupWindow.dismiss();
//                        }
//
//                        @Override
//                        public void onError() {
//                            loadingDialog.dismiss();
//                        }
//                    });
//                }
//            });
//            mMainBottomPopupWindow.showAtLocation(mFlUserSeal, Gravity.BOTTOM, 0, 0);
//        } else {
//            ToastUtil.showCenterToast("无印章");
//        }
    }
}
